import { createRouter, createWebHistory } from "vue-router";
import HomeView from "@/views/HomeView.vue";
import BookDetail from "@/views/BookDetail.vue";
import Register from "@/views/Register.vue";
import ActivateEmail from "@/views/ActivateEmail.vue";
import Login from "@/views/Login.vue";
import ResetPassword from "@/views/ResetPassword.vue";
import PasswordReset from "@/views/PasswordReset";
import Personal from "@/views/Personal.vue";
import ChangePassword from "@/views/ChangePassword.vue";
import store from "@/store";
import Collect from "@/views/Collect.vue";
import Writing from "@/views/Writing.vue";
import Owned from "@/views/Owned.vue";
import ChapterRead from "@/views/ChapterRead.vue";

const routes = [
  {
    path: "/",
    name: "home",
    component: HomeView,
  },
  {
    path: "/book/:id",
    name: "BookDetail",
    component: BookDetail,
  },
  {
    path: "/register",
    name: "Register",
    component: Register,
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/activation/:uid/:token",
    name: "ActivateEmail",
    component: ActivateEmail,
  },
  {
    path: "/reset_password",
    name: "ResetPassword",
    component: ResetPassword,
  },
  {
    path: "/password_reset/:uid/:token",
    name: "PasswordReset",
    component: PasswordReset,
  },
  {
    path: "/personal",
    name: "Personal",
    component: Personal,
    meta: {
      requireLogin: true,
    },
  },
  {
    path: "/change_password",
    name: "ChangePassword",
    component: ChangePassword,
    meta: {
      requireLogin: true,
    },
  },
  {
    path: "/collect",
    name: "Collect",
    component: Collect,
    meta: {
      requireLogin: true,
    },
  },
  {
    path: "/writing",
    name: "Writing",
    component: Writing,
    meta: {
      requireLogin: true,
    },
  },
  {
    path: '/owned',
    name: 'Owned',
    component: Owned,
    meta: { requiresAuth: true }
  },
  {
    path: '/book/:id/article/:chapter_id',
    name: 'ChapterRead',
    component: ChapterRead,
  },
  {
    path: "/about",
    name: "about",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/AboutView.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

// 从本地存储中获取 token
const token = localStorage.getItem("token");
// 如果存在 token,说明用户已登录,提交 mutation 设置登录状态
if (token) {
  store.commit("setLoginStatus", true);
}
// 路由导航守卫
router.beforeEach((to, from, next) => {
  // 如果已经登录,并要访问的页面是登录或注册页,直接跳转主页
  if (store.state.isLogin && (to.name === "Login" || to.name === "Register")) {
    next({ name: "home" });

    // 如果访问的页面需要登录,且未登录,跳转登录页
  } else if (
    to.matched.some((record) => record.meta.requireLogin) &&
    !store.state.isLogin
  ) {
    next({ name: "Login", query: { jump: to.path } });

    // 其他情况直接 next
  } else {
    next();
  }

  // 设置全局标题
  if (to.meta.title) {
    document.title = to.meta.title;
  } else {
    document.title = '书云'; // 设置默认标题
  }
  next();
});

export default router;
